package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.manage.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.manage.MaterialsInventoryMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.manage.MaterialsInventory;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.manage.MaterialsOutbound;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.manage.MaterialsStorage;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.manage.MaterialsInventoryService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;

/**
 * Created by yangsy on 2016/12/8.
 */
public class MaterialsInventoryServiceImpl implements MaterialsInventoryService {
    @Autowired
    private MaterialsInventoryMapper materialsInventoryMapper;


    @Override
    public int insertSelective(MaterialsInventory record) {
        return materialsInventoryMapper.insertSelective(record);
    }



    @Override
    public int updateByPrimaryKeySelective(MaterialsInventory record) {
        return materialsInventoryMapper.updateByPrimaryKeySelective(record);
    }


    @Override
    public MaterialsInventory selectByMaterialsIdAndOrgId(MaterialsInventory materialsInventory) {
        if(materialsInventory == null) return null;
        return materialsInventoryMapper.selectMaterialsInventory(materialsInventory);
    }

    @Override
    public JSONObject list(MaterialsInventory materialsInventory) {
        JSONObject jsonObject = new JSONObject();
        materialsInventory.setLimit( materialsInventory.getLimit() == null ? 10 : materialsInventory.getLimit() );
        materialsInventory.setOffset( materialsInventory.getOffset() == null ? 0 : materialsInventory.getOffset() );
        jsonObject.put("total",materialsInventoryMapper.queryCount(materialsInventory));
        jsonObject.put("rows", JSON.toJSON(materialsInventoryMapper.queryList(materialsInventory)));
        return jsonObject;
    }

    @Override
    public int updateByMaterialsStorageInfo(MaterialsStorage materialsStorage) {
        if(materialsStorage == null) return 0 ;
        int resultCount = 0;
        //Map<String,Object> map = new HashMap<>();
        //  物料Id
        //map.put("materialsId",materialsStorage.getMaterialsId());
        //  库存Id
        //map.put("orgId",materialsStorage.getInventoryId());
        MaterialsInventory _materialsInventory = new MaterialsInventory();
        //  物料Id
        _materialsInventory.setMaterialsId(materialsStorage.getMaterialsId());
        //  库存Id
        _materialsInventory.setOrgId(materialsStorage.getInventoryId());
        MaterialsInventory  materialsInventory = materialsInventoryMapper.selectMaterialsInventory(_materialsInventory);
        if(materialsInventory!=null){
            //修改
            BigDecimal storageNum = materialsStorage.getStorageNum();
            storageNum = storageNum.add(materialsInventory.getInventoryNum());
            materialsInventory.setInventoryNum(storageNum);
            materialsInventory.setOrgId(materialsStorage.getInventoryId());
            resultCount += materialsInventoryMapper.updateByPrimaryKeySelective(materialsInventory);
        }else{
            //新增
            materialsInventory = new MaterialsInventory();
            materialsInventory.setMaterialsInventoryId(UniqueUtil.uuid());
            materialsInventory.setMaterialsId(materialsStorage.getMaterialsId());
            materialsInventory.setInventoryNum(materialsStorage.getStorageNum());
            materialsInventory.setOrgId(materialsStorage.getInventoryId());
            resultCount += materialsInventoryMapper.insertSelective(materialsInventory);
        }
        return resultCount;
    }

    @Override
    public int updateByMaterialsOutboundInfo(MaterialsOutbound materialsOutbound) {
        if(materialsOutbound == null) return 0;
        int resultCount = 0;
        //Map<String,Object> map = new HashMap<>();
        //  物质Id
        //map.put("materialsId",materialsOutbound.getMaterialsId());
        //  所属组织Id
        //map.put("orgId",materialsOutbound.getInventoryId());
       // MaterialsInventory  materialsInventory = materialsInventoryMapper.selectByMaterialsIdAndOrgId(map);
        MaterialsInventory _materialsInventory = new MaterialsInventory();
        //  物料Id
        _materialsInventory.setMaterialsId(materialsOutbound.getMaterialsId());
        //  库存Id
        _materialsInventory.setOrgId(materialsOutbound.getInventoryId());

        MaterialsInventory  materialsInventory = materialsInventoryMapper.selectMaterialsInventory(_materialsInventory);
        if(materialsInventory!=null){
            //修改
            BigDecimal outboundNum = materialsOutbound.getOutboundNum();//出库数量
            materialsInventory.setInventoryNum(materialsInventory.getInventoryNum().subtract(outboundNum));//库存数量
            materialsInventoryMapper.updateByPrimaryKeySelective(materialsInventory);
        }
        return 0;
    }

    @Override
    public MaterialsInventory selectByPrimaryKey(String materialsInventoryId) {
        return materialsInventoryMapper.selectByPrimaryKey(materialsInventoryId);
    }
}
